import axios from 'axios'
import {
	ElMessage,
	ElMessageBox
} from 'element-plus'


// 创建axios实例
const service = axios.create({
	baseURL: "http://127.0.0.1:3000", // api的base_url
	timeout: 120000 // 请求超时时间
})

// request拦截器
service.interceptors.request.use(config => {
	
	return config
}, error => {
	// Do something with request error
	console.log(error) // for debug
	Promise.reject(error)
})

// respone拦截器
service.interceptors.response.use(
	response => {
		/**
		 * code为非200是抛错 可结合自己业务进行修改
		 */
		const res = response.data
		if (res.code !== 200) {
			ElMessage({
				message: res.message || res.msg || res.data.message,
				type: 'error',
				duration: 3 * 1000
			})
			return Promise.reject('error')
		} else {
			return res
		}
	},
	error => {
		console.log('err' + error) // for debug
		ElMessage({
			message: error.message,
			type: 'error',
			duration: 3 * 1000
		})
		return Promise.reject(error)
	}
)

export default service
